﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core;
using System.Data;

namespace DataAccess.Classes
{
    public class PhongTro1
    {
        public int IDPhongTro { get; set; }
        public int ID { get; set; }
        public string Ten { get; set; }
        public string ThongTin { get; set; }
        public decimal GiaTien { get; set; }
        public string HinhDaiDien { get; set; }
        public string HinhChinh { get; set; }
        public bool TinhTrang { get; set; }
        public string IDNguoiDung { get; set; }

        public PhongTro1() { }

        #region Hien thi danh sach
        public static List<PhongTro1> LayDanhSachTheoIDDiaDiem(string id, string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("PhongTro_LayTheoIDDiaDiem", Convert.ToInt32(id),
                                                            GlobalConfiguration.DescriptionLength, Convert.ToInt32(page),
                                                            pageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<PhongTro1>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false) reader.Close();
                howManyPages = 0;
                return new List<PhongTro1>();
            }
        }
        public static PhongTro1 Single(string id)
        {
            try
            {
                return CBO.FillObject<PhongTro1>(DataProvider.Instance.ExecuteReader("PhongTro_Single", id));
            }
            catch { return null; }
        }

        public static List<PhongTro1> TimKiem(string timkiemString, string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("PhongTro_TimKiem", timkiemString, GlobalConfiguration.DescriptionLength, page, GlobalConfiguration.PageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<PhongTro1>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                howManyPages = 0;
                return new List<PhongTro1>();
            }
        }
        public static List<PhongTro1> LayTheoTinhTrang(string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("PhongTro_LayTheoTinhTrang", GlobalConfiguration.DescriptionLength,
                                                            page, GlobalConfiguration.PageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<PhongTro1>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                howManyPages = 0;
                return new List<PhongTro1>();
            }
        }
        #endregion


        #region Them
        public static bool Them(PhongTro1 phongtro)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQueryWithOutput("IDPhongTro", "PhongTro_Them",phongtro.IDPhongTro,phongtro.ID,phongtro.Ten,phongtro.ThongTin,phongtro.GiaTien,phongtro.HinhChinh,phongtro.HinhDaiDien,phongtro.TinhTrang,phongtro.IDNguoiDung);
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {

                return false;
            }
        }
        #endregion


        public static int Dem(string idnguoidung)
        {
            
            try
            {
                object rs = DataProvider.Instance.ExecuteScalar("DemSoBaiDang", idnguoidung);
                return Convert.ToInt32(rs);
            }
            catch
            {
                return Convert.ToInt32(0);
            }
        }
        public static List<PhongTro1> Paging(string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("PhongTro_Paging", page, GlobalConfiguration.PageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);

                reader.NextResult();
                return CBO.FillCollection<PhongTro1>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                howManyPages = 0;
                return new List<PhongTro1>();
            }
        }
        public static bool CapNhat(PhongTro1 phongtro)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQuery("PhongTro_CapNhat", phongtro.IDPhongTro, phongtro.ID, phongtro.Ten, phongtro.ThongTin, phongtro.GiaTien, phongtro.HinhChinh, phongtro.HinhDaiDien, phongtro.TinhTrang);
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {

                return false;
            }
        }

    }
}
